/**
 * 选择人员插件
 * 使用说明：在需要弹出选择人员窗口的标签加上 onclick="openCourseListModal();"
 * 确认选择人员按钮事件需要在使用的页面上实现如下方法:
 function confirmSelectCourse(){
        var checkedCourseIds = $("#checkedCourseIds").val();//选择的人员id字符串集合
        if(checkedCourseIds != ""){
            //根据id集合查询用户信息
            $.post(getCourseListByIdsUrl,{"courseIds":checkedCourseIds},function(data){
                var courseList = data.courseList;
                courseList[0].courseCode;
                courseList[0].courseName;
                courseList[0].courseType;
            });
        }
        closeCourseListModal();
    }

 */
var getCourseListPageUrl = "/lrm/controller/course/getCoursePage";
var getCourseListUrl = "/lrm/controller/course/getCourseList";
var getCourseListByIdsUrl = "/lrm/controller/course/getCourseListByIds";



$(function(){
    var div = '<div id="loadCourseListDiv"></div>';
    $("#mainScroll").after(div);
    loadCourseListModal();
})


function loadCourseListModal(){
    $("#loadCourseListDiv").load(getCourseListPageUrl, function(){
        $("input[name='courseCheckboxAll']").iCheck({
            checkboxClass: "icheckbox_minimal-blue",
            radioClass: "iradio_minimal-blue",
            increaseArea: "20%"
        });
        $("input[name='courseCheckboxAll']").on("ifClicked", function(event){
            if(!$(this).prop("checked")){
                $("input[name='courseCheckboxSub']").iCheck("check");
                var checkedCourses = $("input[name='courseCheckboxSub']:checked");
                var checkedCourseArray = $("#checkedCourseIds").val().split(",");
                var checkedCourseStr = '';
                $.each(checkedCourses ,function(i,checkedCourse){
                    var courseId = $(checkedCourse).val();
                    if($.inArray(courseId, checkedCourseArray) < 0){
                        checkedCourseArray.push(courseId);
                        var courseName = $(checkedCourse).attr("courseName");
                        checkedCourseStr+='<span class="btn btn-default btn-xs" id="checkedCourse_'+courseId+'" style="margin: 0px 5px 5px 0px;">'+courseName+'<span aria-hidden="true" onclick=removeCheckedCourse("'+courseId+'")>&nbsp;&times;</span></span>';
                    }
                });
                $("#checkedCourseIds").val(array_remove("", checkedCourseArray));
                $("#checkedCourseArea").append(checkedCourseStr);
            }else{
                $("input[name='courseCheckboxSub']").iCheck("uncheck");
                var uncheckedCourses = $("input[name='courseCheckboxSub']").not("input:checked");
                $.each(uncheckedCourses,function(i, uncheckedCourse){
                    var courseId = $(uncheckedCourse).val();
                    removeCheckedCourse(courseId);
                });
            }
        });
    });
}

//打开选择讲师窗口
function openCourseListModal(){
    searchCourseList(1);
    $("#course_modal").modal("show");
}
//关闭选择讲师窗口
function closeCourseListModal(){
    $("#course_modal").modal("hide");
}

//加载讲师列表
function searchCourseList(pageNo){
    debugger;
    $("input[name='courseCheckboxAll']").iCheck("uncheck");
    var courseCodeOrName = $("#courseCodeOrName").val();
    var catalogId = $("#catalogId").val();
    var params = {"courseCodeOrName":courseCodeOrName, "catalogId":catalogId,"pageNo":pageNo};
    $.post(getCourseListUrl, params, function(data){
        var courseListStr = "";
        debugger;
        var openPage = data.openPage;
        if(openPage.rows.length > 0){
            var pageNo = openPage.pageNo;
            var pageSize = openPage.pageSize;
            var total = openPage.total;
            var totalPages = openPage.totalPages;
            var checkNum = 0;
            $.each(openPage.rows,function(i,courseInfo){
                var indexNo = (pageNo-1)*pageSize+(i+1);
                var courseId = courseInfo.courseId;
                var courseCode = courseInfo.courseCode;
                var courseName = courseInfo.courseName;
                var courseType = courseInfo.courseType;
                var courseSource = courseInfo.courseSource;
                var speakerName = courseInfo.speakerName;
                var openType = courseInfo.openType;
                var openTypeStr = "";
                if(openType == "allOpen") {
                    openTypeStr = "全部开放";
                } else if(openType == "partOpen"){
                    openTypeStr = "部分开放";
                } else {
                    openTypeStr = "不开放";
                }
                var courseTypeStr = "";
                if(courseType == "online") {
                    courseTypeStr = "在线";
                } else {
                    courseTypeStr = "面授";
                }
                courseListStr += '<tr><td><input class="courseCheckboxSub" type="checkbox" name="courseCheckboxSub" ';
                var checkedCourseArray = $("#checkedCourseIds").val().split(",");
                if($.inArray(courseId, checkedCourseArray)>=0){
                    ++checkNum;
                    courseListStr += 'checked="checked" ';
                }
                courseListStr += 'value="'+ courseId +'" courseName="'+courseName+'"></td><td>'+indexNo+'</td>';
                courseListStr += '<td>'+courseCode+'</td>';
                courseListStr += '<td>'+courseName+'</td>';
                courseListStr += '<td>'+courseTypeStr+'</td>';
                courseListStr += '<td>'+courseSource+'</td>';
                courseListStr += '<td>'+speakerName+'</td>';
                courseListStr += '<td>'+openTypeStr+'</td>';
                courseListStr += '</tr>';
            });
            $("#courseList").html(courseListStr);
            var checkBoxNum = $("input[name='courseCheckboxSub']").length;
            if(checkNum == checkBoxNum){
                $("input[name='courseCheckboxAll']").iCheck("check");
            }
            if(total > pageSize){
                coursePageLimit(pageNo,totalPages,"coursePageLimit");
            }
            $("input[name='courseCheckboxSub']").iCheck({
                checkboxClass: "icheckbox_minimal-blue",
                radioClass: "iradio_minimal-blue",
                increaseArea: "20%"
            });
            $("input[name='courseCheckboxSub']").on("ifClicked", function(event){
                var checkNum = $("input[name='courseCheckboxSub']:checked").length;
                if((checkNum+1)==checkBoxNum){
                    $("input[name='courseCheckboxAll']").iCheck("check");
                }
                var courseId = $(this).val();
                if(!$(this).prop("checked")){
                    var courseName = $(this).attr("courseName");
                    var checkedCourseArray = $("#checkedCourseIds").val().split(",");
                    if($.inArray(courseId, checkedCourseArray) < 0){
                        checkedCourseArray.push(courseId);
                        var courseName = $(this).attr("courseName");
                        var checkedCourseStr='<span class="btn btn-default btn-xs" id="checkedCourse_'+courseId+'" style="margin: 0px 5px 5px 0px;">'+courseName+'<span aria-hidden="true" onclick=removeCheckedCourse("'+courseId+'")>&nbsp;&times;</span></span>';
                        $("#checkedCourseIds").val(array_remove("", checkedCourseArray));
                        $("#checkedCourseArea").append(checkedCourseStr);
                    }
                }else{
                    $("input[name='courseCheckboxAll']").iCheck("uncheck");
                    removeCheckedCourse(courseId);
                }
            });
        }else{
            $("#coursePageBar").html("");
            courseListStr='<tr><td colspan="8" style="vertical-align:middle;"><h4 class="text-center"><small>没有找到相关数据</small></h4></td></tr>';
            $("#courseList").html(courseListStr);
            $("#coursePageLimitDiv").hide();
        }
    })
}

//数组移除某一项
function array_remove(value,array){
    var newArray = new Array();
    $.each(array,function(i,item){
        if(item != value){
            newArray.push(item);
        }
    })
    return newArray;
}

//数组去除重复值
function array_unique(array){
    array = array || [];
    var a = {};
    var len = array.length;
    for (var i=0; i<len;i++){
        var v = array[i];
        if (typeof(a[v]) == 'undefined'){
            a[v] = 1;
        }
    };
    array.length=0;
    for (var x in a){
        array[array.length] = x;
    }
    return array;
}

//移除已选的
function removeCheckedCourse(id){
    $("#checkedCourse_"+id).remove();
    var checkedCourseArray = $("#checkedCourseIds").val().split(",");
    $("#checkedCourseIds").val(array_remove(id, checkedCourseArray));
    $("input[name='courseCheckboxSub'][value='"+id+"']").iCheck("uncheck");
    $("input[name='courseCheckboxAll']").iCheck("uncheck");
}


//分页设置
function coursePageLimit(currentPage,totalPages,pageBarId) {
    var options = {
        currentPage: currentPage,
        totalPages: totalPages,
        numberOfPages: 5,
        bootstrapMajorVersion: 3,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "尾页";
                case "page":
                    return page;
            }
        },
        tooltipTitles: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "尾页";
                case "page":
                    return "第"+page+"页";
            }
        },
        onPageClicked: function (e, originalEvent, type, pageNo) {
            searchCourseList(pageNo);
        }
    }
    $("#"+pageBarId).bootstrapPaginator(options);
    $("#"+pageBarId).addClass("pagination-sm");
}